package com.hq.fxh.No1_Infromation.Video.Fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hq.fxh.No0_ChengQu.Fragment.BaseFragment;
import com.hq.fxh.No1_Infromation.Infor.Fragment.InformationFragment;
import com.hq.fxh.No1_Infromation.Video.Activity.VideoDetailsActivity;
import com.hq.fxh.No1_Infromation.Video.Adapter.Adapter_videolist;
import com.hq.fxh.Modle.GetEntertainmentListRes;
import com.hq.fxh.Modle.GetEntertainmentListResInfo;
import com.hq.fxh.R;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.HttpApi.UserServiceImpl;

import java.util.ArrayList;
import java.util.List;

import rx.Subscriber;

/**
 * /**
 *
 * @author 薛
 * @ClassName VideoFragment
 * @Description :资讯——>视频页面
 * @Created 2017/8/9
 * @Email xlwenhao1858@sina.com
 */


public class VideoFragment extends BaseFragment {
    protected PullToRefreshListView videofragmentList;
    private Adapter_videolist videoAdapter;
    List<GetEntertainmentListResInfo> data;
    InformationFragment inforContext;
    Context context;
    String imageurl = "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1646018908,2787080596&fm=26&gp=0.jpg";
    int page = 1;

    public void setInforContext(InformationFragment inforContext) {

        this.inforContext = inforContext;
    }


    public void fetchData() {
        getData();
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_video;
    }

    @Override
    protected void findView(View view) {
        videofragmentList = (PullToRefreshListView) view.findViewById(R.id.videofragment_list);
        videofragmentList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getContext(), VideoDetailsActivity.class);
                GetEntertainmentListResInfo itemAtPosition = (GetEntertainmentListResInfo) adapterView.getItemAtPosition(i);
                Bundle bundle = new Bundle();
                bundle.putSerializable("data", itemAtPosition);
                intent.putExtra("bundle", bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initData() {
        videofragmentList.setMode(PullToRefreshBase.Mode.BOTH);
        videofragmentList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                fetchData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                ++page;
                fetchData();
            }
        });
        videoAdapter = new Adapter_videolist();
        videofragmentList.setAdapter(videoAdapter);
        ArrayList<String> data = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            data.add(imageurl);
        }
        videoAdapter.setData(null);
        videoAdapter.notifyDataSetChanged();
        fetchData();
    }

    public static VideoFragment newInstance() {

        Bundle args = new Bundle();

        VideoFragment fragment = new VideoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private void initView(View rootView) {

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        LogUtils.e("video_ onHiddenChanged");

        super.onHiddenChanged(hidden);
        if (hidden) {
            inforContext.setHindForFatie(true);
        } else {
            inforContext.setHindForFatie(false);
        }
    }

    protected void getData() {
        LogUtils.e("video_ getData");

        UserServiceImpl.getEntertainmentList(page, 10, 1, new Subscriber<GetEntertainmentListRes>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                videofragmentList.onRefreshComplete();
                LogUtils.e("网络请求错误" + e.getMessage());
            }

            @Override
            public void onNext(GetEntertainmentListRes getEntertainmentListRes) {
                videofragmentList.onRefreshComplete();
                if (data == null) {
                    data = new ArrayList<GetEntertainmentListResInfo>();
                }
                if (page == 1) {
                    data.clear();
                }
                LogUtils.e("网络请求success");
                data.addAll(getEntertainmentListRes.getData());
                videoAdapter.setData(data);
                videoAdapter.notifyDataSetChanged();
            }
        });

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        LogUtils.e("video_ onCreate");
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onStart() {
        LogUtils.e("video_ onStart");

        super.onStart();
    }

    @Override
    public void onPause() {

        super.onPause();
    }

    @Override
    public void onDestroy() {
        LogUtils.e("video_ onDestroy");

        super.onDestroy();
    }
}
